package com.sgcc.ln.pmos.e_ln_order.fragment;

import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sgcc.ln.pmos.arouter.app.AppArouterPath;
import com.sgcc.ln.pmos.arouter.mall.MallArouterPath;
import com.sgcc.ln.pmos.arouter.order.OrderArouterPath;
import com.sgcc.ln.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.ln.pmos.common.bean.BaseResponse;
import com.sgcc.ln.pmos.common.bean.EventCenter;
import com.sgcc.ln.pmos.common.bean.IndentOrContractBean;
import com.sgcc.ln.pmos.common.dialog.CommonDialog;
import com.sgcc.ln.pmos.common.utils.ArguConstant;
import com.sgcc.ln.pmos.common.utils.DateUtils;
import com.sgcc.ln.pmos.common.utils.UserManager;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.ln.pmos.e_ln_order.adapter.IndentAdapter;
import com.sgcc.ln.pmos.e_ln_order.bean.IndentBean;
import com.sgcc.ln.pmos.e_ln_order.contract.IndentSearchContract;
import com.sgcc.ln.pmos.e_ln_order.presenter.IndentSearchPresenter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 订单
 */
public class IndentSearchFragment extends MvpBaseRecycleFragment<IndentOrContractBean, IndentSearchPresenter> implements IndentSearchContract.View {

    private IndentAdapter mIndentAdapter;
    private String searchContent;

    @Override
    public void querySuccess(IndentBean response) {
        if (response != null) {
            if (response.getCode() == 200) {
                if (response.getData() != null) {
                    loadSuccess(response.getData().isHasNextPage(), response.getData().getList());
                }
            } else {
                loadFalied();
            }
        }
    }

    @Override
    public void queryFailed() {
        loadFalied();
    }

    @Override
    public void cancelBargainSuccess(BaseResponse response) {
        refresh(false);
    }

    @Override
    public void userRejectBargainSuccess(BaseResponse response) {
        refresh(false);
    }

    @Override
    public void beforeSubmitSuccess(BaseResponse response) {
        refresh(false);
    }

    @Override
    public void orderCheckSuccess(String mRelationId) {
        ARouter.getInstance()
                .build(OrderArouterPath.ORDER_CONFIRMORDER)
                .withString("mRelationId", mRelationId)
                .navigation();
    }


    @Override
    public void onResume() {
        super.onResume();
        onChangePage();
    }


    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(EventCenter eventCenter) {
        if (eventCenter.getEventCode() == ArguConstant.LOGOUT) {
            onChangePage();
        } else if (eventCenter.getEventCode() == ArguConstant.REFRESH_INDENT) {
            refresh(false);
        } else if (eventCenter.getEventCode() == ArguConstant.INTERFACE_LOGOUT) {
            onChangePage();
        }
    }

    private void onChangePage() {
        if (UserManager.isLogin()) {
            ll_content.setVisibility(View.VISIBLE);
            not_login_layout.setVisibility(View.GONE);
//            refresh(false);
        } else {
            View notLoginView = getLayoutInflater().inflate(R.layout.not_login_indent_layout, not_login_layout, false);
            TextView mTvLogin = (TextView) notLoginView.findViewById(R.id.tv_login);
            mTvLogin.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    ARouter.getInstance()
                            .build(AppArouterPath.APP_LOGIN)
                            .navigation();
                }
            });
            not_login_layout.addView(notLoginView);
            not_login_layout.setVisibility(View.VISIBLE);
            ll_content.setVisibility(View.GONE);
        }
    }

    @Override
    protected IndentSearchPresenter createPresenter() {
        return new IndentSearchPresenter();
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mIndentAdapter = new IndentAdapter(mContext);
        mIndentAdapter.setItemClickListener(new IndentAdapter.OnItemClickListener() {
            @Override
            public void OnCompanyNameClickListener(int position, IndentOrContractBean bean) {
                ARouter.getInstance()
                        .build(MallArouterPath.MALL_STOREHOMEPAGE)
                        .withString("membersSdId", bean.getMembersSdId())
                        .navigation();
            }

            @Override
            public void OnConfirmClickListener(int position, IndentOrContractBean bean) {
                mPresenter.orderCheck(bean.getRelationId(), bean.getMembersSdId(), bean.getCombId(),
                        DateUtils.getStrng_to_Times_ymd_2(bean.getStartDate()), DateUtils.getStrng_to_Times_ymd_2(bean.getEndDate()));

            }

            @Override
            public void OnUndoBargainingClickListener(int position, IndentOrContractBean bean) {
                CommonDialog dialog = new CommonDialog(mContext);
                dialog.setTitle("提示");
                dialog.setMessage("撤销议价后需要重新发起，是否确认撤销？");
                dialog.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
                    @Override
                    public void onPositiveClick() {
                        dialog.dismiss();
                        String relationId = bean.getRelationId();
                        mPresenter.cancelBargain(relationId);
                    }

                    @Override
                    public void onNegtiveClick() {
                        dialog.dismiss();
                    }
                }).show();
            }

            @Override
            public void OnRejectedBargainingClickListener(int position, IndentOrContractBean bean) {
                CommonDialog dialog = new CommonDialog(mContext);
                dialog.setTitle("提示");
                dialog.setMessage("驳回议价后需要重新发起，是否确认驳回？");
                dialog.setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
                    @Override
                    public void onPositiveClick() {
                        dialog.dismiss();
                        mPresenter.userRejectBargain(bean.getRelationId());
                    }

                    @Override
                    public void onNegtiveClick() {
                        dialog.dismiss();
                    }
                }).show();
            }

            @Override
            public void OnLookInfoListener(int position, IndentOrContractBean bean) {
                ARouter.getInstance()
                        .build(OrderArouterPath.ORDER_ORDERDETAILS)
                        .withString("relCode", bean.getRelCode())
                        .withString("from", "1")
                        .navigation();


            }
        });
        return mIndentAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        if (UserManager.isLogin()) {
            mPresenter.getIndentList(listSize, isloading, searchContent);
        }
    }

    public void setSearchContent(String searchContent) {
        this.searchContent = searchContent;
        refresh(true);
    }

}
